import twitter._

import java.io.File
import scala.collection.mutable.HashMap

val fileName = "test0002.json"
//
//JSONReader.apply(new File("src/main/resources/" + fileName))

val iter = AvroIter.apply[Tweet](new java.io.File("src/main/resources/" + fileName + ".avro"))
//val wordcount = iter.map(_.text.getOrElse("")).map(_.split("\\s+").size).foldLeft(0)(_+_)
//println("words: " + wordcount)

def wordCount(iter: Iterator[Tweet]) = {
    iter.map(_.text.getOrElse("")).map(_.split("\\s+").size).foldLeft(0)(_+_)
}

def userToTweets(iter: Iterator[Tweet]) = {
    iter.foldLeft(new HashMap[String, List[Tweet]])((map, tweet) => { 
        tweet.user match {
            case Some(user) => 
                user.name match {
                    case Some(name) =>
                        val buf = map.get(name).getOrElse(Nil)
                        map += name -> (buf ::: List(tweet))
                    case None =>
                        println("Found user with no name: " + user)
                }
            case None =>
                println("Found tweet with no user: " + tweet)
        }
        map
    })
}

//println("userToTweets: " + userToTweets(iter))
val userMap = userToTweets(iter)

